package com.gotloop.jdo;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * A Loop is a short sample sent by User. It is played over and over.
 * If sound file is not well loopable. User can play on startTime and endTime positions.
 * 
 * @author jibhaine
 *
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class Loop implements Serializable {

	/**
	 * Generated serial version UID.
	 */
	private static final long serialVersionUID = 8885752096938470021L;

	/**
	 * Loop primary key.
	 */
    @PrimaryKey
    @Persistent
	private Key key;
	
	/**
	 * Loop author.
	 */
	@Persistent(mappedBy = "uploadedLoops")
	private UserProfile author;

	/**
	 * Loop title.
	 */
	@Persistent
	private String title;
	
	/**
	 * Loop title in url format.
	 */
	@Persistent
	private String url;

	/**
	 * Loop description.
	 */
	@Persistent
	private String description;

	/**
	 * List of tags.
	 */
	@Persistent
	private List<Tag> tags;

	/**
	 * Loop start Time.
	 */
	@Persistent
	private double startTime;

	/**
	 * Loop end Time.
	 */
	@Persistent
	private double endTime;

	/**
	 * Beats per minute of the Loop.
	 */
	@Persistent
	private int bpm;
	
	/**
	 * True if published.
	 */
	@Persistent
	private boolean published;

	/**
	 * Date of loop creation.
	 */
	@Persistent
	private Date creationDate;

	/**
	 * Date of loop modification.
	 */
	@Persistent
	private Date modificationDate;

	/**
	 * Sound file data.
	 */
	@Persistent
	private Blob data;
	
	/**
	 * List of comments made on this loop.
	 */
	@Persistent
	private List<Comment> comments;
	
	/**
	 * Public constructor to initialize the Loop.
	 * @param pAuthor		User who sent the loop
	 * @param pTitle			title of the loop
	 * @param pUrl			title in url format
	 * @param pDescription	description of the loop
	 * @param pTags			list of tags
	 * @param pData			data
	 */
	public Loop(UserProfile pAuthor, String pTitle, String pUrl, String pDescription, List<Tag> pTags, Blob pData) {
		super();
		this.author = pAuthor;
		this.title = pTitle;
		this.url = pUrl;
		this.key = KeyFactory.createKey(Loop.class.getSimpleName(), pUrl);
		this.description = pDescription;
		this.tags = pTags;
		this.data = pData;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Loop [title=" + title + ", url=" + url + ", description="
				+ description + ", tags=" + tags + ", startTime=" + startTime
				+ ", endTime=" + endTime + ", bpm=" + bpm + ", published="
				+ published + ", creationDate=" + creationDate + "]";
	}

	/**
	 * Get accessor for key.
	 * @return the key to get
	 */
	public Key getKey() {
		return key;
	}

	/**
	 * Set accessor for key.
	 * @param pKey the key to set
	 */
	public void setKey(Key pKey) {
		this.key = pKey;
	}

	/**
	 * Get accessor for author.
	 * @return the author to get
	 */
	public UserProfile getAuthor() {
		return author;
	}

	/**
	 * Set accessor for author.
	 * @param pAuthor the author to set
	 */
	public void setAuthor(UserProfile pAuthor) {
		this.author = pAuthor;
	}

	/**
	 * Get accessor for title.
	 * @return the title to get
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * Set accessor for title.
	 * @param pTitle the title to set
	 */
	public void setTitle(String pTitle) {
		this.title = pTitle;
	}

	/**
	 * Get accessor for url.
	 * @return the url to get
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * Set accessor for url.
	 * @param pUrl the url to set
	 */
	public void setUrl(String pUrl) {
		this.url = pUrl;
	}

	/**
	 * Get accessor for description.
	 * @return the description to get
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Set accessor for description.
	 * @param pDescription the description to set
	 */
	public void setDescription(String pDescription) {
		this.description = pDescription;
	}

	/**
	 * Get accessor for tags.
	 * @return the tags to get
	 */
	public List<Tag> getTags() {
		return tags;
	}

	/**
	 * Set accessor for tags.
	 * @param pTags the tags to set
	 */
	public void setTags(List<Tag> pTags) {
		this.tags = pTags;
	}

	/**
	 * Get accessor for startTime.
	 * @return the startTime to get
	 */
	public double getStartTime() {
		return startTime;
	}

	/**
	 * Set accessor for startTime.
	 * @param pStartTime the startTime to set
	 */
	public void setStartTime(double pStartTime) {
		this.startTime = pStartTime;
	}

	/**
	 * Get accessor for endTime.
	 * @return the endTime to get
	 */
	public double getEndTime() {
		return endTime;
	}

	/**
	 * Set accessor for endTime.
	 * @param pEndTime the endTime to set
	 */
	public void setEndTime(double pEndTime) {
		this.endTime = pEndTime;
	}

	/**
	 * Get accessor for bpm.
	 * @return the bpm to get
	 */
	public int getBpm() {
		return bpm;
	}

	/**
	 * Set accessor for bpm.
	 * @param pBpm the bpm to set
	 */
	public void setBpm(int pBpm) {
		this.bpm = pBpm;
	}

	/**
	 * Get accessor for published.
	 * @return the published to get
	 */
	public boolean isPublished() {
		return published;
	}

	/**
	 * Set accessor for published.
	 * @param pPublished the published to set
	 */
	public void setPublished(boolean pPublished) {
		this.published = pPublished;
	}

	/**
	 * Get accessor for creationDate.
	 * @return the creationDate to get
	 */
	public Date getCreationDate() {
		return creationDate;
	}

	/**
	 * Set accessor for creationDate.
	 * @param pCreationDate the creationDate to set
	 */
	public void setCreationDate(Date pCreationDate) {
		this.creationDate = pCreationDate;
	}

	/**
	 * Get accessor for modificationDate.
	 * @return the modificationDate to get
	 */
	public Date getModificationDate() {
		return modificationDate;
	}

	/**
	 * Set accessor for modificationDate.
	 * @param pModificationDate the modificationDate to set
	 */
	public void setModificationDate(Date pModificationDate) {
		this.modificationDate = pModificationDate;
	}

	/**
	 * Get accessor for data.
	 * @return the data to get
	 */
	public Blob getData() {
		return data;
	}

	/**
	 * Set accessor for data.
	 * @param pData the data to set
	 */
	public void setData(Blob pData) {
		this.data = pData;
	}

	/**
	 * Get accessor for comments.
	 * @return the comments to get
	 */
	public List<Comment> getComments() {
		return comments;
	}

	/**
	 * Set accessor for comments.
	 * @param pComments the comments to set
	 */
	public void setComments(List<Comment> pComments) {
		this.comments = pComments;
	}
	
	
}
